package org.glut.cloud.serviceRating.service.impl;

import org.glut.cloud.serviceRating.dto.MaintenanceRecords;
import org.glut.cloud.serviceRating.dto.ServiceEvaluationDTO;
import org.glut.cloud.serviceRating.dto.Users;
import org.glut.cloud.serviceRating.feign.MaintenanceRecordsService;
import org.glut.cloud.serviceRating.service.IServiceEvaluationService;
import org.glut.cloud.serviceRating.entity.ServiceEvaluation;
import org.glut.cloud.serviceRating.mapper.ServiceEvaluationMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author qjc
 * @since 2024-08-13 16:33
 */
@Service
public class ServiceEvaluationServiceImpl extends ServiceImpl<ServiceEvaluationMapper, ServiceEvaluation> implements IServiceEvaluationService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ServiceEvaluationMapper serviceEvaluationMapper;

    // 注入Feign客户端
    @Autowired
    private MaintenanceRecordsService maintenanceRecordsService;

    public List<ServiceEvaluationDTO> getEvaluationsWithUserName() {
        // 获取所有评价记录
        List<ServiceEvaluation> evaluations = this.list();

        return evaluations.stream().map(evaluation -> {
            System.out.println("=============" + evaluation.getSuId());
            // 使用 usId 获取用户信息
            String userServiceUrl = "http://repair-UserManage/users/" + evaluation.getSuId();

            Users user = restTemplate.getForObject(userServiceUrl, Users.class);

            // 将用户信息与评价信息组合在一起
            ServiceEvaluationDTO dto = new ServiceEvaluationDTO();
            dto.setEvaId(evaluation.getEvaId());
            dto.setTaskId(evaluation.getTaskId());
            dto.setMaintenanceRecordsId(evaluation.getMaintenanceRecordsId());
            dto.setUserNo(evaluation.getUserNo());
            dto.setUserName(user != null ? user.getName() : "未知"); // 设置用户名，如果用户信息不存在则显示"未知"
            LocalDateTime localDateTime = evaluation.getEvaTime();
            Date date = new Date(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
            dto.setEvaTime((Date) date);
            dto.setRating(evaluation.getRating());
            dto.setComments(evaluation.getComments());

            return dto;
        }).collect(Collectors.toList());
    }


    /**
     * 添加评价
     *
     * @param serviceEvaluation
     */
    @Override
    public boolean addEvaluation(ServiceEvaluation serviceEvaluation) {
        int result =  serviceEvaluationMapper.insert(serviceEvaluation);
        if (result == 1) {
            // 更新维修记录的评价状态 远程
            maintenanceRecordsService.updateStatus(serviceEvaluation.getMaintenanceRecordsId());
        }
        return result == 1;
    }

    /**
     * 查看评价
     *
     * @param maintenanceRecordsId
     */
    @Cacheable(value = "evaluation::selectById", key = "#maintenanceRecordsId")
    @Override
    public ServiceEvaluation selectById(Integer maintenanceRecordsId) {
        ServiceEvaluation serviceEvaluation = serviceEvaluationMapper.selectById(maintenanceRecordsId);
        return serviceEvaluation;
    }

    /**
     * 查询维修记录 远程
     * */
    @Override
    public List<MaintenanceRecords> selectRecords(Integer userNo, String evaStatus) {
        return maintenanceRecordsService.select_1(userNo, evaStatus);
    }
}

